
const multer = require('koa-multer')
const path = require('path')
const Router = require('koa-router')

const account = require('./actions/account')
const auth = require('./middlewares/auth')
const photo = require('./actions/photo')
const uuid = require('uuid')

const router = new Router()

function getPageParams (context) {
    return {
        pageIndex: parseInt(context.query.pageIndex) || 1,
        pageSize: parseInt(context.query.pageSize)  || 10
    }
}

async function responseOK(ctx, next) {
    ctx.body = {
        status: 0,
    }
    await next()
}

/**
 * 小程序登录，接受小程序登录获取的 code
*/
router.get('/login', async (context, next) => {
    const code = context.query.code
    context.logger.info(`[login] 用户登陆Code为${code}`)
    context.body = {
        status:0,
        data:await account.login(code)
    }
})

/**
 * 修改用户信息
*/
router.put('/user', auth, async (context, next) => {
    context.logger.info(`[user] 修改用户信息，用户ID${context.stale.user.id}, 修改的内容为${JSON.stringify(context.request.body)}`)
    await account.update(context.state.user.id, context.request.body)
    await next()
}, responseOK)

/**
 * 获取当前登录的用户信息
*/
router.put('/get', auth, async (context, next) => {
    context.body = {
        status:0,
        data: context.state.user
    }
})